<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in rolist" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hsubmit">确定</el-button>
      <el-button @click="hcancel">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/settings'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    employeId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // 双向绑定多选框状态
      roleIds: [],
      //  渲染数据
      rolist: []
    }
  },
  created() {
    this.loadtRoles()
  },
  methods: {
    // 全部角色函数
    async loadtRoles() {
      try {
        const res = await getRoles({ page: 1, pagesize: 200 })
        this.rolist = res.data.rows
        console.log(res, '角色')
        // 获取角色信息
        const UserDeta = await getUserDetailById(this.employeId)
        console.log(UserDeta, '2')
        // 设置选中角色的选中状态
        this.roleIds = UserDeta.data.roleIds
        console.log(UserDeta, '1')
      } catch (error) {
        console.log(error)
      }
    },
    // 确定
    async hsubmit() {
      try {
        await assignRoles({ id: this.employeId, roleIds: this.roleIds })
      } catch (error) {
        console.log(error)
      }
      this.$emit('close')
    },
    // 取消
    hcancel() {
      this.$emit('close')
    }
  }
}
</script>
